/*
 * Copyright 2019 The ZuSmart Project
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zusmart.base.logging.support;

import java.io.Serializable;

import com.zusmart.base.logging.Logger;
import com.zusmart.base.logging.LoggerLevel;
import com.zusmart.base.util.StringUtils;

/**
 * @author Administrator
 *
 */
public abstract class AbstractLogger implements Logger, Serializable {

	public static final String EXCEPTION_MESSAGE = "Unexpected exception:";

	private static final long serialVersionUID = -1632367282325534763L;

	private final String name;

	protected AbstractLogger(String name) {
		if (null == name) {
			throw new NullPointerException("loggerName");
		}
		this.name = name;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public boolean isEnabled(LoggerLevel level) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			return this.isTraceEnabled();
		} else if (level == LoggerLevel.DEBUG) {
			return this.isDebugEnabled();
		} else if (level == LoggerLevel.INFO) {
			return this.isInfoEnabled();
		} else if (level == LoggerLevel.WARN) {
			return this.isWarnEnabled();
		} else if (level == LoggerLevel.ERROR) {
			return this.isErrorEnabled();
		}
		throw new Error();
	}

	@Override
	public void trace(Throwable cause) {
		this.trace(EXCEPTION_MESSAGE, cause);
	}

	@Override
	public void debug(Throwable cause) {
		this.debug(EXCEPTION_MESSAGE, cause);
	}

	@Override
	public void info(Throwable cause) {
		this.info(EXCEPTION_MESSAGE, cause);
	}

	@Override
	public void warn(Throwable cause) {
		this.warn(EXCEPTION_MESSAGE, cause);
	}

	@Override
	public void error(Throwable cause) {
		this.error(EXCEPTION_MESSAGE, cause);
	}

	@Override
	public void log(LoggerLevel level, String msg) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			this.trace(msg);
		} else if (level == LoggerLevel.DEBUG) {
			this.debug(msg);
		} else if (level == LoggerLevel.INFO) {
			this.info(msg);
		} else if (level == LoggerLevel.WARN) {
			this.warn(msg);
		} else if (level == LoggerLevel.ERROR) {
			this.error(msg);
		}
		throw new Error();
	}

	@Override
	public void log(LoggerLevel level, String msg, Object arg) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			this.trace(msg, arg);
		} else if (level == LoggerLevel.DEBUG) {
			this.debug(msg, arg);
		} else if (level == LoggerLevel.INFO) {
			this.info(msg, arg);
		} else if (level == LoggerLevel.WARN) {
			this.warn(msg, arg);
		} else if (level == LoggerLevel.ERROR) {
			this.error(msg, arg);
		}
		throw new Error();
	}

	@Override
	public void log(LoggerLevel level, String msg, Object argA, Object argB) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			this.trace(msg, argA, argB);
		} else if (level == LoggerLevel.DEBUG) {
			this.debug(msg, argA, argB);
		} else if (level == LoggerLevel.INFO) {
			this.info(msg, argA, argB);
		} else if (level == LoggerLevel.WARN) {
			this.warn(msg, argA, argB);
		} else if (level == LoggerLevel.ERROR) {
			this.error(msg, argA, argB);
		}
		throw new Error();
	}

	@Override
	public void log(LoggerLevel level, String msg, Object... args) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			this.trace(msg, args);
		} else if (level == LoggerLevel.DEBUG) {
			this.debug(msg, args);
		} else if (level == LoggerLevel.INFO) {
			this.info(msg, args);
		} else if (level == LoggerLevel.WARN) {
			this.warn(msg, args);
		} else if (level == LoggerLevel.ERROR) {
			this.error(msg, args);
		}
		throw new Error();
	}

	@Override
	public void log(LoggerLevel level, String msg, Throwable cause) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			this.trace(msg, cause);
		} else if (level == LoggerLevel.DEBUG) {
			this.debug(msg, cause);
		} else if (level == LoggerLevel.INFO) {
			this.info(msg, cause);
		} else if (level == LoggerLevel.WARN) {
			this.warn(msg, cause);
		} else if (level == LoggerLevel.ERROR) {
			this.error(msg, cause);
		}
		throw new Error();
	}

	@Override
	public void log(LoggerLevel level, Throwable cause) {
		if (null == level) {
			throw new Error();
		}
		if (level == LoggerLevel.TRACE) {
			this.trace(cause);
		} else if (level == LoggerLevel.DEBUG) {
			this.debug(cause);
		} else if (level == LoggerLevel.INFO) {
			this.info(cause);
		} else if (level == LoggerLevel.WARN) {
			this.warn(cause);
		} else if (level == LoggerLevel.ERROR) {
			this.error(cause);
		}
		throw new Error();
	}

	@Override
	public String toString() {
		return String.format("%s(%s)", StringUtils.getSimpleClassName(this), this.getName());
	}

}